Element.addMethods({
    'find': function(){
        var args = $A(arguments);
        var element = $(args.shift());
        return Selector.findChildElements(element, args);
    },

    'fireEvt': function(element, event){
        var evt;
        if(document.createEvent)
        {
            var type;
            switch(event)
            {
                case 'change':
                    type = 'HTMLEvents';
                    break;
                case 'keyup':
                case 'keydown':
                case 'keypress':
                    type = 'KeyboardEvent';
                    break;
                case 'hover':
                case 'click':
                case 'dblclick':
                    type = 'MouseEvents';
                    break;
            }
            // dispatch for firefox + others
            evt = document.createEvent(type);
            // event type,bubbling,cancelable
            evt.initEvent(event, true, true );
            return !element.dispatchEvent(evt);
        }
        else
        {
            // dispatch for IE
            evt = document.createEventObject();
            return element.fireEvent('on'+event , evt);
        }
    },

    'popUp': function(element)
    {
        var height = element.getHeight();
        var style = element.readAttribute('style').replace('display: none;', '');
        element.makeClipping().setStyle({height: '0px', width:'0px', opacity:'0'}).show();
        element.morph('opacity:1;width:100%;height:'+height+'px', {
            duration:0.5,
            afterFinish:function(){
                element.setAttribute('style', style);
            }
        });
    },

    'popOut': function(element)
    {
        element.morph('opacity:0;height:0px', {
            duration:0.5,
            afterFinish:function(){
                element.hide();
            }
        });
    }
});

String.prototype.hash = function(separator){
    var out = new Hash();
    var array = this.split(separator);
    array.each(function(item){
        if (item) out.set(item, item);
    });
    return out;
};

var msgBox;
var loader;
var msgFlash;
var Client = Class.create({});
Object.extend(Client, {
	get: function(path)
	{
		document.location = Client.getUrl(path);
	},

    open: function(path, windowName)
    {
        var url = Client.getUrl(path);
        window.open(url, windowName);
    },

    openFront: function(path, windowName)
    {
        var url = '[:url_app:]/' + path;
        window.open(url, windowName);
    },

    ajax: function(url, options)
    {
        if (!msgBox)
        {
            msgBox = new Element('div', {'class':'msgBox'});
            $(document.body).insert({'top':msgBox});
        }
        if (!loader)
        {
            loader = new Element('div', {'class':'pageLoader'}).update('loading...').hide();
            msgBox.insert({'top':loader});
        }
        jQuery(loader).show(400);
        var callback = options.onComplete;
        options.onComplete = function(transport){
			var response = Client.log(transport, options);
            if (callback) callback(response);
            jQuery(loader).hide(400);
        };
        new Ajax.Request(url, options);
    },

    getUrl: function(path)
    {
        return '[:url_app:]/' + path;
    },

    log: function(transport, options)
    {
        var response = transport.responseText.split('<!-- debug-info -->');
        if (response.length>1 && $$('.log').length>0)
            $$('.log').first().insert({before:response[1]});
        var tmp = response[0].split('<!-- msg-flash -->');
        if (tmp.length>1)
        {
            this.updateMsgBox(tmp[1]);
        }
        return tmp[0];
    },

    updateMsgBox: function(e)
    {
        if (!msgBox)
        {
            msgBox = new Element('div', {'class':'msgBox'});
            $(document.body).insert({'top':msgBox});
        }
        if (!msgFlash)
        {
            msgFlash = new Element('div');
            msgBox.insert(msgFlash);
        }
        msgFlash.hide();
        msgFlash.update(e);
        jQuery(msgFlash).show(400);
    },

    getQuery: function(name)
    {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp(regexS);
        var results = regex.exec(window.location.href);
        if(results == null) return '';
        else return results[1];
    }
});

document.observe('dom:loaded', function(){
	$$('.messagebox').each(function(e){
        Client.updateMsgBox(e);
    });
});

function classDefined(c)
{
    return typeof(c) == 'object';
}

function empty(array)
{
    return array.length < 1;
}

function getLiPosition(li)
{
    var ul = li.up('ul');
    var count = 0;
    var out = 0;
    ul.find('li').each(function(e){
        if (e == li) out = count;
        count++;
    });
    return out;
}